<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container>

      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="6">
            <a-form-model-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billcode">
              <a-input v-model="model.billcode" placeholder="请输入单据编号" :disabled="formDisabled"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="单据日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="billdate">
              <j-date placeholder="请选择单据日期" v-model="model.billdate" :disabled="formDisabled" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="地区" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cityid">
              <!-- <a-input v-model="model.cityid" placeholder="请输入地区" ></a-input> -->
              <j-area-linkage type="cascader" :disabled="formDisabled" v-model="model.cityid" placeholder="请输入省市区" />
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="vendorid">
              <!-- <a-input v-model="model.vendorid" placeholder="请输入供应商" ></a-input> -->
              <c-select-vendor v-model="model.vendorid" :disabled="formDisabled" :buttons="false"></c-select-vendor>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="询价单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xjdbillcode">
              <a-input v-model="model.xjdbillcode" :disabled="formDisabled" placeholder="请输入询价单号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="车牌号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="plate">
              <a-input v-model="model.plate" :disabled="formDisabled" placeholder="请输入车牌号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="事故号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accidentNumber">
              <a-input v-model="model.accidentNumber" :disabled="formDisabled" placeholder="请输入事故号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="定损员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dsy">
              <a-input v-model="model.dsy" :disabled="formDisabled" placeholder="请输入定损员"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="定损员联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="dsylinktel">
              <a-input v-model="model.dsylinktel" :disabled="formDisabled" placeholder="请输入定损员联系方式"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="修理厂地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xlcaddress">
              <a-input v-model="model.xlcaddress" :disabled="formDisabled" placeholder="请输入修理厂地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="修理厂联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="xlclink">
              <a-input v-model="model.xlclink" :disabled="formDisabled" placeholder="请输入修理厂联系方式"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="6">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-input v-model="model.remarks" :disabled="formDisabled" placeholder="请输入备注"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="事故图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="accidentimagesString">
              <j-image-upload isMultiple v-model="model.accidentimagesString" :disabled="formDisabled"
                :bizPath="imgsrc"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="行驶证图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="licenseimageString">
              <j-image-upload v-model="model.licenseimageString" :disabled="formDisabled"></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
    <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="采购订单分录" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table keep-source :ref="refKeys[0]" :loading="scmpoitemTable.loading" :columns="scmpoitemTable.columns"
          :disabled="this.model.deliveyflag === '2'" :dataSource="scmpoitemTable.dataSource" :maxHeight="300"
          :rowNumber="true" :rowSelection="true" :toolbar="true" :alwaysEdit="true" />
      </a-tab-pane>
      <a-tab-pane tab="采购附件" :key="refKeys[1]" :forceRender="true">
        <scmpoattach-form ref="scmpoattachForm" @validateError="validateError"></scmpoattach-form>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

import { getAction } from '@/api/manage'
import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
import { JVXETypes } from '@/components/jeecg/JVxeTable'
import { getRefPromise, VALIDATE_FAILED } from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import ScmpoattachForm from './ScmpoattachForm.vue'
export default {
  name: 'ScmpoForm',
  mixins: [JVxeTableModelMixin],
  components: {
    JFormContainer,
    ScmpoattachForm
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      model: {
      },
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      validatorRules: {
      },
      refKeys: ['scmpoitem', 'scmpoattach',],
      tableKeys: ['scmpoitem',],
      activeKey: 'scmpoitem',
      // 采购订单分录
      scmpoitemTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '配件编码',
            key: 'matcode',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '配件名称',
            key: 'matname',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '配件数量',
            key: 'matcount',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '配件品质',
            key: 'quanlity',
            type: JVXETypes.sel_search,
            dictCode: 'pjpz',
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '采购单价',
            key: 'price',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '采购金额',
            key: 'value',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '备注',
            key: 'remarks',
            type: JVXETypes.input,
            width: "200px",
            placeholder: '请输入${title}',
            defaultValue: '',
          },
        ]
      },

      // 采购附件
      scmpoattachTable: {
        loading: false,
        dataSource: [],
        columns: [
        ]
      },
      url: {
        add: "/rfq.po/scmpo/add",
        edit: "/rfq.po/scmpo/edit",
        queryById: "/rfq.po/scmpo/queryById",
        scmpoitem: {
          list: '/rfq.po/scmpo/queryScmpoitemByMainId'
        },
        scmpoattach: {
          list: '/rfq.po/scmpo/queryScmpoattachByMainId'
        },
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  computed: {
    formDisabled () {
      return this.disabled
    },
  },
  created () {
  },
  methods: {
    addBefore () {
      this.scmpoitemTable.dataSource = []
    },
    getAllTable () {
      let values = this.tableKeys.map(key => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter () {
      this.$nextTick(() => {
        this.$refs.scmpoattachForm.initFormData(this.url.scmpoattach.list, this.model.id)
      })
      // 加载子表数据
      if (this.model.id) {
        let params = { id: this.model.id }
        this.requestSubTableData(this.url.scmpoitem.list, params, this.scmpoitemTable)
      }
    },
    //校验所有一对一子表表单
    validateSubForm (allValues) {
      if (this.model.deliveyflag === '2') {
        this.$message.error("已完成，不允许修改！");
        return;
      }
      return new Promise((resolve, reject) => {
        Promise.all([
        ]).then(() => {
          resolve(allValues)
        }).catch(e => {
          if (e.error === VALIDATE_FAILED) {
            // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
            this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
          } else {
            console.error(e)
          }
        })
      })
    },
    /** 整理成formData */
    classifyIntoFormData (allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        scmpoitemList: allValues.tablesValue[0].tableData,
        scmpoattachList: this.$refs.scmpoattachForm.getFormData(),
      }
    },
    validateError (msg) {
      this.$message.error(msg)
    },

  }
}
</script>

<style scoped></style>